package com.wd.live.module_wide.view.activity

import android.Manifest
import android.content.pm.PackageManager
import android.media.MediaPlayer
import android.net.Uri
import android.view.View
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.stx.xhb.xbanner.transformers.ZoomPageTransformer
import com.wd.live.module_base.net.bean.wide.FindRecommendConstellationBean
import com.wd.live.module_base.ui.base.BaseActivity

import com.wd.live.module_wide.R
import com.wd.live.module_wide.databinding.ActivityConstellationBinding
import com.wd.live.module_wide.view.adapter.FindCommendAdapter
import com.wd.live.module_wide.viewmodel.ConstellationViewModel

@Route(path = "/wide/constellation_activity")
class ConstellationActivity : BaseActivity<ConstellationViewModel, ActivityConstellationBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_constellation
    }
    private var labsConstellation= mutableListOf<FindRecommendConstellationBean>()
    override fun initData() {
        checkNeedPermissions()
        initWelcome()
        //设置页数
        binding.vpCon.offscreenPageLimit = labsConstellation.size
        //设置布局 定制切换效果
//        binding.vpCon.setPageTransformer(true,ZoomPageTransformer())
        binding.vpCon.setPageTransformer(true, ZoomPageTransformer())

        viewModel.getFindRecommendConstellation(10, 100)
        viewModel.dataConstellation.observe(this) {
            it.result?.let { it1 ->
                labsConstellation.addAll(it1)
                binding.vpCon.adapter = FindCommendAdapter(this@ConstellationActivity,it1)
            }
//            binding.vpCon.adapter?.notifyDataSetChanged()
        }
//        }


    }

    fun initWelcome(){
        binding.videoPlay.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.playstarsignvideo))

        binding.videoPlay.start()
        //视频监听 结束
        binding.videoPlay.setOnCompletionListener(object : MediaPlayer.OnCompletionListener {
            override fun onCompletion(p0: MediaPlayer?) {
                //重复播放
                //  vdb.squarePlayVideoId.start()
                ToastUtils.showShort("播放结束")
                //隐藏界面
                binding.videoPlay.visibility = View.GONE
                //显示列表
                binding.listCon.visibility= View.VISIBLE
                //销毁
                if (binding.videoPlay != null) {
                    binding.videoPlay.stopPlayback()
                }
            }
        });
    } private fun checkNeedPermissions() {

        //6.0以上需要动态申请权限
        if ((ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                    != PackageManager.PERMISSION_GRANTED) || (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
                    != PackageManager.PERMISSION_GRANTED) || (ContextCompat.checkSelfPermission(
                this,
                Manifest.permission.READ_EXTERNAL_STORAGE
            )
                    != PackageManager.PERMISSION_GRANTED)
        ) {
            //多个权限一起申请
            ActivityCompat.requestPermissions(
                this, arrayOf<String>(
                    Manifest.permission.CAMERA,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
                ), 1
            )
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (binding.videoPlay != null) {
            binding.videoPlay.stopPlayback()
        }
    }

    override fun getViewModelClass(): Class<ConstellationViewModel> {
       return ConstellationViewModel::class.java
    }


}